<template>
    <div ref="chatList" class="chat-list">
        <div
            class="more"
            v-show="curMessageList.length >= 30"
            @click="moreMessage"
        >
            查看更多
        </div>
        <div class="prompt flex-center-1">
            <i class="el-icon-info"></i>
            <div>
                官方提醒：请勿轻信不明跳转链接、二维码支付及其他联系方式，如脱离小竹财税平台交易，支付打款。资金安全、纠纷处理、完善交付将均不受保障！
            </div>
        </div>

        <div v-for="(item, index) in curMessageList" :key="index">
            <template v-if="item.senderUserID == userInfo.id">
                <messageRight :item="item" :type="gettype(item)"></messageRight>
            </template>
            <template v-else>
                <messageLeft
                    :item="item"
                    :type="gettype(item)"
                    :avatar="
                        conversationObj.conversationAvatarUrl
                            ? conversationObj.conversationAvatarUrl
                            : 'https://api.xiaozhucaishui.com/assets/img/avatar.png'
                    "
                ></messageLeft>
            </template>
        </div>

        <!-- <messageLeft type="1"></messageLeft>
        <messageRight type="1"></messageRight> -->
        <div class="height0"></div>
    </div>
</template>

<script>
import messageLeft from "@/components/service/messageLeft.vue";
import messageRight from "@/components/service/messageRight.vue";

import { mapState } from "vuex";
export default {
    name: "chatList",
    components: {
        messageLeft,
        messageRight,
    },
    props: {
        curMessageList: {
            type: Array,
            default: () => {
                return [];
            },
        },
        conversationObj: {
            type: Object,
            default: () => {
                return {};
            },
        },
    },
    data() {
        return {
            oMessageID: null,
            gettype: function (item) {
                return JSON.parse(item.extendedData).type + "";
            },
        };
    },
    computed: {
        ...mapState("common", {
            userInfo: (state) => state.userInfo,
        }),
    },
    watch: {
        curMessageList: function (n, o) {
            if (
                n &&
                n.length > 1 &&
                (!this.oMessageID ||
                    this.oMessageID != n[n.length - 1].messageID)
            ) {
                setTimeout(() => {
                    this.scrollBottom();
                }, 200);
            }
            if (n && n.length > 1) {
                const messageID = n[n.length - 1].messageID;
                this.oMessageID = messageID;
            }
        },
    },
    created() {},
    mounted() {},
    methods: {
        scrollBottom() {
            this.$nextTick(() => {
                this.$refs.chatList.scrollTop =
                    this.$refs.chatList.scrollHeight;
            });
        },
        moreMessage() {
            this.$emit("moreMessage");
        },
    },
};
</script>

<style lang="scss" scoped>
.more {
    cursor: pointer;
    margin: 0 auto;
    margin-bottom: 30px;
    width: 80px;
    height: 28px;
    text-align: center;
    line-height: 28px;
    border: 1px solid #dcdfe6;
    background-color: #ffffff;
    border-radius: 20px;
    color: #606266;
    font-size: 12px;
    &:active {
        opacity: 0.8;
    }
}

.chat-list {
    height: 500px;
    background-color: #f4f4f4;
    padding-top: 25px;
    overflow: scroll;

    &::-webkit-scrollbar {
        width: 4px;
    }
    &::-webkit-scrollbar-thumb {
        border-radius: 10px;
        box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
        background: #b2e6fa;
    }
    &::-webkit-scrollbar-track {
        border-radius: 0;
    }
}

.prompt {
    width: 744px;
    height: 66px;
    background: #fff9fa;
    border: 1px solid #f9989f;
    margin: 0 auto 40px;
    color: #cd505e;
    padding: 0 28px;
    box-sizing: border-box;
    position: sticky;
    top: -13px;
    z-index: 9;
    div {
        font-size: 12px;
        line-height: 17px;
    }
    i {
        font-size: 16px;
        margin-right: 13px;
    }
}
.height0{
    height: 0;
}
</style>
